package lar.game.gamestate;

import javax.microedition.khronos.opengles.GL11;

import lar.system.engine.MainProgram;
import lar.system.engine.UI;
import lar.system.opengl.GLEmptyQuad;
import lar.system.sound.Sound;
import lar.system.R;

public class FadeOutFadeIn implements GameState  
{
	GameState prev,next;
	int fadeOut,fadeIn;  
	int fadeOutInit, fadeInInit;
	public FadeOutFadeIn(GameState prev, GameState next)
	{
		this.prev = prev;
		this.next = next;
		fadeOut = 30;
		fadeIn = 30;
		fadeInInit = fadeIn;
		fadeOutInit = fadeOut;
		
	}
	
	public void gameFrame() 
	{
		UI.needRendering = true;
		if(fadeOut > 0) fadeOut--;
		
		else fadeIn--;
		if(fadeIn <= 0)
		{
			MainProgram.gameLoop.ChangeState(next);
		}
	}

	public void Draw(GL11 gl) 
	{
		
		if(fadeOut > 0)
		{
			float d = (float)(fadeOutInit-fadeOut)/(float)fadeOutInit;
			prev.Draw(gl);
			float asp = MainProgram.glSurfaceView.getAspectRatio();
			gl.glBindTexture(GL11.GL_TEXTURE_2D, 0);
			gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
			gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
			gl.glFrontFace(GL11.GL_CW);
			gl.glVertexPointer(3, GL11.GL_FLOAT, 0, GLEmptyQuad.vertexBuffer);
			gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, GLEmptyQuad.textureBuffer);
			gl.glPushMatrix();
			gl.glTranslatef(0f, 0f, -1.0f);
			gl.glScalef(asp,1f, 1.0f);
			gl.glColor4f(0,0,0,d);
			gl.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, GLEmptyQuad.vertices.length / 3);
			gl.glColor4f(1f, 1f, 1f, 1f);
			gl.glPopMatrix();
			gl.glDisableClientState(GL11.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		}
		else
		{
			
			float d = (float)(fadeInInit-fadeIn)/(float)fadeInInit;
			next.Draw(gl);
			float asp = MainProgram.glSurfaceView.getAspectRatio();
			gl.glBindTexture(GL11.GL_TEXTURE_2D, 0);
			gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
			gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
			gl.glFrontFace(GL11.GL_CW);
			gl.glVertexPointer(3, GL11.GL_FLOAT, 0, GLEmptyQuad.vertexBuffer);
			gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, GLEmptyQuad.textureBuffer);
			gl.glPushMatrix();
			gl.glTranslatef(0f, 0f, -1.0f);
			gl.glScalef(asp,1f, 1.0f);
			gl.glColor4f(0,0,0,1-d);
			gl.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, GLEmptyQuad.vertices.length / 3);
			gl.glColor4f(1f, 1f, 1f, 1f);
			gl.glPopMatrix();
			gl.glDisableClientState(GL11.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		}
	}

	public void onPause() 
	{	
	
	}

	public void onResume() 
	{
		
	}

	public boolean CheckBounds(float x, float y) 
	{
		return false;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBack() {
		// TODO Auto-generated method stub
		
	}


}
